package com.zyj.test.month1.day7;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Yijia Zhang
 * @create 2024-04-28 22:38
 */
public class code0013 {
    public int romanToInt(String s) {
        Map<Character, Integer> map = new HashMap<>();
        map.put('I',1);
        map.put('V', 5);
        map.put('X', 10);
        map.put('L', 50);
        map.put('C', 100);
        map.put('D', 500);
        map.put('M', 1000);
        int nowMin = Integer.MAX_VALUE;
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            int v = map.get(c);
            if (v > nowMin) {
                res -= (nowMin << 1);
            }
            res += v;
            nowMin = v;
        }
        return res;
    }
}
